package endpoint

import (
	"context"
	"github.com/go-kit/kit/endpoint"
	"go-kit-demo/service"
)

// 定义Request、Response格式，中间件嵌套，调用服务，返回结果

type UserRequest struct {
	Id int `json:"id"`
}

type UserResponse struct {
	Name string `json:"name"`
}

// 核心endpoint，调用service
// 初始化endpoint
func GenerateUserEndpoint(userService service.IUserService) endpoint.Endpoint {

	// 接收context、request处理，调用service，完成response响应
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		// 转换 request -> UserRequest
		r := request.(UserRequest)

		// 调用 service
		name := userService.GetName(r.Id)

		// 返回 response
		return UserResponse{name}, nil
	}
}
